package pxene.phantomJs.util;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SingleDriver {
	private static WebDriver driver;

	private static AtomicInteger ai = new AtomicInteger();
	private static final String userAgent = "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1";

	static {
		System.setProperty("phantomjs.binary.path",
				"C:/Users/chenjinghui/Desktop/phantomjs-2.1.1-windows/bin/phantomjs.exe");
		System.setProperty("phantomjs.page.settings.userAgent", userAgent);
	}

	// private SingleDriver() {
	// // Exists only to defeat instantiation.
	// }

	/**
	 * 单例
	 * 
	 * @return
	 */
	public static WebDriver getInstance() {
		System.out.println("--------" + ai.intValue());
		if (driver == null) {
			ArrayList<String> cliArgsCap = new ArrayList<String>();
			// cliArgsCap.add("--proxy=localhost:8080");
			// cliArgsCap.add("--proxy-auth=username:password");
			// cliArgsCap.add("--proxy-type=http");
			// cliArgsCap.add("--load-images=false"); // 不加载图片
			// cliArgsCap.add("--web-security=false");
			// DesiredCapabilities capabilities =
			// DesiredCapabilities.phantomjs();
			// capabilities.setJavascriptEnabled(true); //执行js
			// capabilities.setCapability("takesScreenshot", false);
			// capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
			// cliArgsCap);

			DesiredCapabilities dCaps = new DesiredCapabilities();
			dCaps.setJavascriptEnabled(true);
			dCaps.setCapability("takesScreenshot", false);

			driver = new PhantomJSDriver(dCaps);
			// driver = new PhantomJSDriver();
			System.out.println("--------------------------------------");
			return driver;
		} else {
			return driver;
		}
	}

	/**
	 * 防止被墙，定期重启driver，更换参数 IP代理可以在此轮转
	 * 
	 * @return
	 */
	public WebDriver getDriver(int count) {
		ai.incrementAndGet();
		if (driver == null || ai.intValue() > count) {
			ArrayList<String> cliArgsCap = new ArrayList<String>();
			// cliArgsCap.add("--proxy=localhost:8080");
			// cliArgsCap.add("--proxy-auth=username:password");
			// cliArgsCap.add("--proxy-type=http");
			cliArgsCap.add("--load-images=false"); // 不加载图片
			cliArgsCap.add("--ssl-protocol=any");
			DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
			capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);

			// 关闭已经启动的driver
			if (driver != null) {
				driver.close();
			}
			driver = new PhantomJSDriver(capabilities);
			System.out.println("--------------------start driver------------------");
			ai.set(0); // 初始化计数器
			return driver;
		} else {
			return driver;
		}
	}

	/**
	 * 关闭
	 */
	public static void closeDriver() {
		if (driver != null) {
			driver.close();
		}
	}

	public static void main(String[] args) {
		// WebDriver driver = SingleDriver.getInstance();
		SingleDriver singleDriver = new SingleDriver();
		for (int i = 0; i < 30; i++) {
			singleDriver.getDriver(10).get("http://www.autohome.com.cn/beijing/");
			String title = driver.getTitle();
			System.out.println(title);
		}
		SingleDriver.closeDriver();
	}
}
